<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-10
 * Time: 16:28
 */

namespace App\Service;


use App\Model\LinGroupPermission;
use App\Model\LinPermission;

class PermissionService
{
    public function getAllPermissions(){
        return LinPermission::all(['id','name','module','mount']);
    }

    /**
     * 通过分组id得到分组的权限
     * @param int $groupId
     */
    public function getPermissionByGroupId($groupId){
        $groupPermissions = LinGroupPermission::where('group_id',$groupId)->get(['permission_id'])->toArray();
        if(!$groupPermissions){
            return [];
        }
        $permissionIds = array_column($groupPermissions,'permission_id');
        return LinPermission::whereIn('id',$permissionIds)->orderBy('module')->orderBy('index')->get(['id','name','module'])->toArray();

    }

    /**
     * 通过分组id得到分组的权限
     * @param array $groupIds
     */
    public function getPermissionByGroupIds(array $groupIds){
        $groupPermissions = LinGroupPermission::whereIn('group_id',$groupIds)->get('permission_id')->toArray();
        $permissionIds = array_column($groupPermissions,'permission_id');
        return  LinPermission::whereIn('id',$permissionIds)->where('mount',1)->get(['module','name'])->toArray();
    }

    public function structuringPermissions(){

    }
}